/// created by hujintao
/// created at 2023/5/18
/// 描述:

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';

import '../../image_enum.dart';
/// 图片加载插件:支持本地与网络图片,以及各种形状
class HKImage extends StatelessWidget {
  final String? imageUrl;
  final double? width;
  final double? height;
  final HKImageShape? shape;
  final PlaceholderWidgetBuilder? placeholder;
  final LoadingErrorWidgetBuilder? errorWidget;

  //是否需要imageLoader,默认false
  final bool? needImageBuilder;
  final ImageWidgetBuilder? imageBuilder;

  // imageBuilder圆角
  final double? imageBuilderRadius;
  final BoxFit? fit;

  //像素偏移
  final Matrix4? pixel;

  // 设置图片圆角
  final BorderRadiusGeometry? borderRadius;

  /// 本地图片是否需要增加前缀默认加前缀
  final bool? needPrefix;

  //默认前缀
  final String? prefix;

  /// 本地图片格式:默认png
  final HKImageFormat? format;

  const HKImage(
      {Key? key,
      this.imageUrl = "",
      this.width,
      this.height,
      this.shape = HKImageShape.normal,
      this.placeholder,
      this.errorWidget,
      this.needImageBuilder = false,
      this.imageBuilder,
      this.imageBuilderRadius = 5,
      this.fit = BoxFit.cover,
      this.pixel,
      this.borderRadius,
      this.needPrefix = true,
      this.prefix = 'images/',
      this.format = HKImageFormat.png})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    Widget target = Container();
    Widget image = CachedNetworkImage(
      height: height,
      width: width,
      fit: fit,
      imageBuilder: imageBuilder ??
          (needImageBuilder!
              ? (context, imageProvider) => Container(
                    decoration: ShapeDecoration(
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadiusDirectional.circular(
                            imageBuilderRadius!),
                      ),
                      image: DecorationImage(
                        image: imageProvider,
                        fit: BoxFit.cover,
                      ),
                    ),
                  )
              : null),
      placeholder: placeholder ??
          (
            BuildContext context,
            String url,
          ) =>
              Container(color: Colors.grey[200]),
      errorWidget: errorWidget ??
          (
            BuildContext context,
            String url,
            dynamic error,
          ) =>
              const Icon(Icons.error),
      imageUrl: imageUrl ?? "",
    );
    switch (shape) {
      case HKImageShape.normal:
        target = image;
        break;
      case HKImageShape.oval:
        target = ClipOval(
          child: image,
        );
        break;
      case HKImageShape.corner:
        target = ClipRRect(
            //是ClipRRect，不是ClipRect
            borderRadius: borderRadius ?? BorderRadius.circular(5),
            child: image);
        break;
      case HKImageShape.pixel:
        target = Container(
          transform: pixel ?? Matrix4.translationValues(0.3, 0, 0),
          child: image,
        );
        break;
      default:
        target = image;
        break;
    }
    return target;
  }
}

